﻿/* 
 * CacheManagerDesign.cs
 * 
 * Author - Ankit Gupta and Gaurav Aggarwal
 * 
 * This file handles component design of Cache Manager tab.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using Microsoft.Win32;

partial class CacheManagerControl
{
    public Label cellServDBLabel = new Label();
    public Label defaultCellLabel = new Label();
    public Label rootVolumeLabel = new Label();
    public Label rootMountPointLabel = new Label();
    public Label sysnamesLabel = new Label();
    public Label encryptLabel = new Label();
    public Label ansiFileNamesLabel = new Label();
    public Label hiddenDotFilesLabel = new Label();
    public Label callbackPortLabel = new Label();
    public TextBox cellServDBTextBox = new TextBox();
    public TextBox defaultCellTextBox = new TextBox();
    public TextBox rootVolumeTextBox = new TextBox();
    public TextBox rootMountPointTextBox = new TextBox();
    public TextBox sysnamesTextBox = new TextBox();
    public TextBox callbackPortTextBox = new TextBox();
    public CheckBox encryptCheckBox = new CheckBox();
    public CheckBox ansiFileNamesCheckBox = new CheckBox();
    public CheckBox hiddenDotFilesCheckBox = new CheckBox();
    public Button browseButton = new Button();
    public Button applyButton = new Button();
    public Button cancelButton = new Button();
    public FolderBrowserDialog folderbrowser = new FolderBrowserDialog();

    public RegistryKey serviceParameters = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters", true);
    public RegistryKey clientKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client", true);

    public CacheManagerControl()
    {
        // Cell Server Database directory
        cellServDBLabel.Location = new System.Drawing.Point(15, 20);
        cellServDBLabel.Name = "cellServDBLabel";
        cellServDBLabel.Size = new System.Drawing.Size(140, 15);
        cellServDBLabel.Text = "CellServDB directory";
        
        cellServDBTextBox.Location = new System.Drawing.Point(155, 20);
        cellServDBTextBox.Name = "cellServDBTextBox";
        cellServDBTextBox.Size = new System.Drawing.Size(180, 20);
        cellServDBTextBox.ReadOnly = true;
        cellServDBTextBox.Text = clientKey.GetValue("CellServDBDir", "(undefined)").ToString();

        // Browse directories 
        browseButton.Location = new System.Drawing.Point(355, 17);
        browseButton.Name = "browseButton";
        browseButton.Size = new System.Drawing.Size(60, 23);
        browseButton.Text = "Browse...";
        browseButton.Click += new System.EventHandler(button_Click);

        // Default AFS Cell
        defaultCellLabel.Location = new System.Drawing.Point(15, 60);
        defaultCellLabel.Name = "defaultCellLabel";
        defaultCellLabel.Size = new System.Drawing.Size(140, 15);
        defaultCellLabel.Text = "Default cell";

        defaultCellTextBox.Location = new System.Drawing.Point(155, 60);
        defaultCellTextBox.Name = "defaultCellTextBox";
        defaultCellTextBox.Size = new System.Drawing.Size(180, 20);
        defaultCellTextBox.Text = serviceParameters.GetValue("Cell", "(none)").ToString();

        // Root Volume of AFS Cell
        rootVolumeLabel.Location = new System.Drawing.Point(15, 100);
        rootVolumeLabel.Name = "rootVolumeLabel";
        rootVolumeLabel.Size = new System.Drawing.Size(140, 15);
        rootVolumeLabel.Text = "Root volume";

        rootVolumeTextBox.Location = new System.Drawing.Point(155, 100);
        rootVolumeTextBox.Name = "rootVolumeTextBox";
        rootVolumeTextBox.Size = new System.Drawing.Size(180, 20);
        rootVolumeTextBox.Text = serviceParameters.GetValue("RootVolume", "root.afs").ToString();

        // Root Mount Point of AFS Cell
        rootMountPointLabel.Location = new System.Drawing.Point(15, 140);
        rootMountPointLabel.Name = "rootMountPointLabel";
        rootMountPointLabel.Size = new System.Drawing.Size(140, 15);
        rootMountPointLabel.Text = "Name of root mount point";

        rootMountPointTextBox.Location = new System.Drawing.Point(155, 140);
        rootMountPointTextBox.Name = "rootMountPointTextBox";
        rootMountPointTextBox.Size = new System.Drawing.Size(180, 20);
        rootMountPointTextBox.Text = serviceParameters.GetValue("MountRoot", "/afs").ToString();

        // AFS System Names
        sysnamesLabel.Location = new System.Drawing.Point(15, 180);
        sysnamesLabel.Name = "sysnamesLabel";
        sysnamesLabel.Size = new System.Drawing.Size(140, 15);
        sysnamesLabel.Text = "AFS sysnames";

        sysnamesTextBox.Location = new System.Drawing.Point(155, 180);
        sysnamesTextBox.Name = "sysnamesTextBox";
        sysnamesTextBox.Size = new System.Drawing.Size(180, 20);
        sysnamesTextBox.Text = serviceParameters.GetValue("SysName", "").ToString();

        // Enable/Disable encrypting Communication
        encryptCheckBox.Location = new System.Drawing.Point(15, 220);
        encryptCheckBox.Name = "encryptCheckBox";
        encryptCheckBox.Size = new System.Drawing.Size(20, 20);
        encryptCheckBox.Checked = Convert.ToBoolean((int)serviceParameters.GetValue("SecurityLevel", 1));

        encryptLabel.Location = new System.Drawing.Point(35, 220);
        encryptLabel.Name = "encryptLabel";
        encryptLabel.Size = new System.Drawing.Size(200, 15);
        encryptLabel.Text = "Encrypt communication";

        // Enable/Disable Storing ANSI Filenames 
        ansiFileNamesCheckBox.Location = new System.Drawing.Point(15, 260);
        ansiFileNamesCheckBox.Name = "ansiFileNamesCheckBox";
        ansiFileNamesCheckBox.Size = new System.Drawing.Size(20, 20);
        ansiFileNamesCheckBox.Checked = Convert.ToBoolean((int)clientKey.GetValue("StoreAnsiFilenames", 0));

        ansiFileNamesLabel.Location = new System.Drawing.Point(35, 260);
        ansiFileNamesLabel.Name = "ansiFileNamesLabel";
        ansiFileNamesLabel.Size = new System.Drawing.Size(200, 15);
        ansiFileNamesLabel.Text = "Store ANSI filenames";

        // Enable/Disable Marking dot files as hidden 
        hiddenDotFilesCheckBox.Location = new System.Drawing.Point(15, 300);
        hiddenDotFilesCheckBox.Name = "hiddenDotFilesCheckBox";
        hiddenDotFilesCheckBox.Size = new System.Drawing.Size(20, 20);
        hiddenDotFilesCheckBox.Checked = Convert.ToBoolean((int)serviceParameters.GetValue("HideDotFiles", 1));

        hiddenDotFilesLabel.Location = new System.Drawing.Point(35, 300);
        hiddenDotFilesLabel.Name = "hiddenDotFilesLabel";
        hiddenDotFilesLabel.Size = new System.Drawing.Size(200, 15);
        hiddenDotFilesLabel.Text = "Mark dot-files as hidden";

        // Callback Port
        callbackPortLabel.Location = new System.Drawing.Point(15, 340);
        callbackPortLabel.Name = "callbackPortLabel";
        callbackPortLabel.Size = new System.Drawing.Size(140, 15);
        callbackPortLabel.Text = "Callback port";

        callbackPortTextBox.Location = new System.Drawing.Point(155, 340);
        callbackPortTextBox.Name = "callbackPortTextBox";
        callbackPortTextBox.Size = new System.Drawing.Size(180, 20);
        callbackPortTextBox.Text = ((int)serviceParameters.GetValue("CallBackPort", 7001)).ToString();

        applyButton.Location = new System.Drawing.Point(20, 460);
        applyButton.Name = "applyButton";
        applyButton.Size = new System.Drawing.Size(100, 23);
        applyButton.Text = "Apply";
        applyButton.Click += new System.EventHandler(applyButton_Click);

        cancelButton.Location = new System.Drawing.Point(140, 460);
        cancelButton.Name = "cancelButton";
        cancelButton.Size = new System.Drawing.Size(100, 23);
        cancelButton.Text = "Cancel";
        cancelButton.Click += new System.EventHandler(cancelButton_Click);
    }

    private void button_Click(object sender, EventArgs e)
    {
        if (folderbrowser.ShowDialog() == DialogResult.OK)
        {
            cellServDBTextBox.Text = folderbrowser.SelectedPath;
        }
    }
}